<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-row>
        <!-- 项目编号 -->
        <el-col :span="12">
          <el-form-item label="项目编号" prop="projectCode">
            <el-select v-model="dataForm.projectCode" placeholder="请选择项目编号" class="label_len" :disabled="selectDis">
              <el-option v-for="item in projectList" :key="item.prProjectNumber"
                :label="item.prProjectNumber + item.prProjectName" :value="item.prProjectNumber">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 测定日期 -->
        <el-col :span="12">
          <el-form-item label="测定日期" prop="assayData">
            <el-date-picker v-model="dataForm.assayData" type="datetime" placeholder="测定日期" format="yyyy-MM-dd"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="拖拉机" prop="matchingTractor">
        <el-input v-model="dataForm.matchingTractor" placeholder="配套拖拉机"></el-input>
      </el-form-item>
      <el-form-item label="测定地点" prop="measurementAddress">
        <el-input v-model="dataForm.measurementAddress" placeholder="测定地点"></el-input>
      </el-form-item>
      <el-form-item label="作业挡位" prop="workGear">
        <el-input v-model="dataForm.workGear" placeholder="作业挡位"></el-input>
      </el-form-item>
      <el-table style="width: 100%" :data="tableData">
        <el-table-column label="项目名称" prop="itemName" width="150"></el-table-column>

        <!-- 往测量值 -->
        <el-table-column label="往">
          <template slot-scope="scope">
            <el-form-item :prop="scope.row.pastProp">
              <el-input v-model="dataForm[scope.row.pastProp]" :placeholder="'请输入' + scope.row.itemName + '往测量值'" type="number" @input="calculatePastVelocity(),calculateAvgVelocity()">
              </el-input>
            </el-form-item>
          </template>
        </el-table-column>

        <!-- 反测量值 -->
        <el-table-column label="反">
          <template slot-scope="scope">
            <el-form-item :prop="scope.row.antiProp">
              <el-input v-model="dataForm[scope.row.antiProp]" :placeholder="'请输入' + scope.row.itemName + '反测量值'" type="number" @input="calculateAntiVelocity(),calculateAvgVelocity()">
              </el-input>
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>

      <!-- 表格数据 -->
      <el-form-item v-for="(item, index) in tableData" :key="index" style="display: none;">
        <el-input v-model="item.pastValue"></el-input>
        <el-input v-model="item.antiValue"></el-input>
      </el-form-item>
      <p style="color: red">植被覆盖率：</p>
      <el-row style="margin-top: 20px">
        <el-col :span="12">
          <el-form-item label="平均速度计算值">
            <el-input v-model="dataForm.computeAvgVelocity" :disabled="true" placeholder="计算平均速度" type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="平均速度校验值">
            <el-input v-model="dataForm.avgVelocity" placeholder="请输入平均速度校验值" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        projectCode: '',
        matchingTractor: '',
        measurementAddress: '',
        workGear: '',
        assayData: '',
        pastLength: '',
        antiLength: '',
        pastTime: '',
        antiTime: '',
        pastVelocity: '',
        antiVelocity: '',
        avgVelocity: '',
        detectPeople: '',
        recorders: '',
        checkers: '',
        computeAvgVelocity:''
      },
      selectDis:false,
      projectList:[],
      tableData: [
        { itemName: '测量长度 m', pastProp: 'pastLength', antiProp: 'antiLength', pastValue: '', antiValue: '' },
        { itemName: '时间 s', pastProp: 'pastTime', antiProp: 'antiTime', pastValue: '', antiValue: '' },
        { itemName: '作业速度 m/s', pastProp: 'pastVelocity', antiProp: 'antiVelocity', pastValue: '', antiValue: '' }
      ],
      dataRule: {
        projectCode: [
          { required: true, message: '项目编号不能为空', trigger: 'blur' }
        ],
        matchingTractor: [
          { required: true, message: '配套拖拉机不能为空', trigger: 'blur' }
        ],
        measurementAddress: [
          { required: true, message: '测定地点不能为空', trigger: 'blur' }
        ],
        workGear: [
          { required: true, message: '作业挡位不能为空', trigger: 'blur' }
        ],
        assayData: [
          { required: true, message: '测定日期不能为空', trigger: 'blur' }
        ],
        pastLength: [
          { required: true, message: '往测区长度不能为空', trigger: 'blur' }
        ],
        antiLength: [
          { required: true, message: '反测区长度不能为空', trigger: 'blur' }
        ],
        pastTime: [
          { required: true, message: '往时间不能为空', trigger: 'blur' }
        ],
        antiTime: [
          { required: true, message: '反时间不能为空', trigger: 'blur' }
        ],
        pastVelocity: [
          { required: true, message: '往作业速度不能为空', trigger: 'blur' }
        ],
        antiVelocity: [
          { required: true, message: '反作业速度不能为空', trigger: 'blur' }
        ],
        avgVelocity: [
          { required: true, message: '平均速度不能为空', trigger: 'blur' }
        ],
        detectPeople: [
          { required: true, message: '检测人不能为空', trigger: 'blur' }
        ],
        recorders: [
          { required: true, message: '记录人不能为空', trigger: 'blur' }
        ],
        checkers: [
          { required: true, message: '校核人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted (){
    this.getprojectList()
  },
  methods: {
    //计算平均速度 (pastVelocity+antiVelocity)/2
    calculateAvgVelocity(){
      const pastVelocity = parseFloat(this.dataForm.pastVelocity) || 0;
      const antiVelocity = parseFloat(this.dataForm.antiVelocity) || 0;

      const avg = (pastVelocity + antiVelocity)/2;
      this.dataForm.computeAvgVelocity = avg.toFixed(2);
      this.dataForm.avgVelocity = avg.toFixed(2)
    },

    calculatePastVelocity() {
      const { pastLength, pastTime } = this.dataForm;
      // 确保时间不为0以避免除以0的错误
      const pastVelocity = pastTime ? (pastLength / pastTime).toFixed(2) : 0;
      // 可以选择计算平均值或者只显示其中一个值
      this.dataForm.pastVelocity = pastVelocity;
    },

    calculateAntiVelocity() {
      const { antiLength, antiTime } = this.dataForm;
      const antiVelocity = antiTime ? (antiLength / antiTime).toFixed(2) : 0;
      // 可以选择计算平均值或者只显示其中一个值
      this.dataForm.antiVelocity = antiVelocity;
    },
    //获取项目列表
    getprojectList() {
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getProjectNumber`),
        method: 'post',
        data: this.$http.adornData({
        })
      }).then((data) => {
        if (data && data.code === 0) {
          this.projectList = data.productList
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.selectDis = false
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/speedoperationtable/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            if (data && data.code === 0) {
              this.dataForm.projectCode = data.speedOperationTable.projectCode
              this.dataForm.matchingTractor = data.speedOperationTable.matchingTractor
              this.dataForm.measurementAddress = data.speedOperationTable.measurementAddress
              this.dataForm.workGear = data.speedOperationTable.workGear
              this.dataForm.assayData = data.speedOperationTable.assayData
              this.dataForm.pastLength = data.speedOperationTable.pastLength
              this.dataForm.antiLength = data.speedOperationTable.antiLength
              this.dataForm.pastTime = data.speedOperationTable.pastTime
              this.dataForm.antiTime = data.speedOperationTable.antiTime
              this.dataForm.pastVelocity = data.speedOperationTable.pastVelocity
              this.dataForm.antiVelocity = data.speedOperationTable.antiVelocity
              this.dataForm.avgVelocity = data.speedOperationTable.avgVelocity
              this.dataForm.detectPeople = data.speedOperationTable.detectPeople
              this.dataForm.recorders = data.speedOperationTable.recorders
              this.dataForm.checkers = data.speedOperationTable.checkers
              this.dataForm.computeAvgVelocity = ''
              this.selectDis = true
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/speedoperationtable/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'projectCode': this.dataForm.projectCode,
              'matchingTractor': this.dataForm.matchingTractor,
              'measurementAddress': this.dataForm.measurementAddress,
              'workGear': this.dataForm.workGear,
              'assayData': this.dataForm.assayData,
              'pastLength': this.dataForm.pastLength,
              'antiLength': this.dataForm.antiLength,
              'pastTime': this.dataForm.pastTime,
              'antiTime': this.dataForm.antiTime,
              'pastVelocity': this.dataForm.pastVelocity,
              'antiVelocity': this.dataForm.antiVelocity,
              'avgVelocity': this.dataForm.avgVelocity,
              'detectPeople': this.dataForm.detectPeople,
              'recorders': this.dataForm.recorders,
              'checkers': this.dataForm.checkers
            })
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.warning(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
